package com.gzyj.jtt808.v2013.protocol;

import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2013.enums.BusinessDataType;
import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.io.Serializable;
import java.nio.charset.Charset;

@Data
public class JTT808_0x0107 extends Package implements Serializable {

    private Integer TerminalType;//终端类型
    private String MakerId;//制造商 ID 5个字节
    private String TerminalModel;//终端型号 20个字节
    private String TerminalId;//终端ID 7个字节
    private String Terminal_SIM_ICCID;//终端 SIM 卡 ICCID BCD 10个字节
    private String Terminal_Hardware_Version_Num;//终端硬件版本号
    private String Terminal_Firmware_Version_Num;//终端固件版本号
    private Byte GNSSModule;//GNSS 模块属性
    private Byte CommunicationModule;//通信模块属性



    public JTT808_0x0107(){
        super(BusinessDataType.查询终端属性应答.getCode());
    }

    @Override
    public void encodeImpl(ByteBuf buf) {
        buf.writeShort(this.TerminalType);
        buf.writeBytes(HexBytesUtil.WriteStringPadRight(this.MakerId,5));
        buf.writeBytes(HexBytesUtil.WriteStringPadRight(this.TerminalModel,20));
        buf.writeBytes(HexBytesUtil.WriteStringPadRight(this.TerminalId,7));
        buf.writeBytes(HexBytesUtil.WriteBCD(this.Terminal_SIM_ICCID,20));
        buf.writeByte(this.Terminal_Hardware_Version_Num.getBytes(Charset.forName("GBK")).length);
        buf.writeCharSequence(this.Terminal_Hardware_Version_Num,Charset.forName("GBK"));
        buf.writeByte(this.Terminal_Firmware_Version_Num.getBytes(Charset.forName("GBK")).length);
        buf.writeCharSequence(this.Terminal_Firmware_Version_Num,Charset.forName("GBK"));
        buf.writeByte(this.GNSSModule);
        buf.writeByte(this.CommunicationModule);
    }

    @Override
    public JTT808_0x0107 decodeImpl(ByteBuf buf) {
        this.TerminalType= buf.readUnsignedShort();
        this.MakerId=HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf,5));
        this.TerminalModel=HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf,20));
        this.TerminalId=HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf,7));
        this.Terminal_SIM_ICCID=HexBytesUtil.ReadBCD(HexBytesUtil.readByOffset(buf,10),20);
        int len=buf.readByte();
        this.Terminal_Hardware_Version_Num=HexBytesUtil.parseString( HexBytesUtil.readByOffset(buf,len));
        len=buf.readByte();
        this.Terminal_Firmware_Version_Num=HexBytesUtil.parseString( HexBytesUtil.readByOffset(buf,len));
        this.GNSSModule=buf.readByte();
        this.CommunicationModule=buf.readByte();

        return this;
    }
}
